package com.niiwoo.civet.trade.service.local.fanlitou;

import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.collect.Sets;
import com.niiwoo.civet.account.dto.common.BankCardDTO;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.civet.base.enums.FeeTypeEnum;
import com.niiwoo.civet.base.enums.OrderTradeSourceEnum;
import com.niiwoo.civet.base.enums.RefundOrderStatusEnum;
import com.niiwoo.civet.base.enums.RefundTypeEnum;
import com.niiwoo.civet.trade.dao.entity.*;
import com.niiwoo.civet.trade.dao.mapper.*;
import com.niiwoo.civet.trade.dto.ProjectBorrowerPlanNewDTO;
import com.niiwoo.civet.trade.dto.ProjectRepayTotalAmountDTO;
import com.niiwoo.civet.trade.dto.RefundOrderTradeSourcePeriodDTO;
import com.niiwoo.civet.trade.dto.request.fanlitou.FanLiTouRequestDTO;
import com.niiwoo.civet.trade.dto.response.cardniu.CardNiuLoanCreditAmountDTO;
import com.niiwoo.civet.trade.dto.response.cardniu.RepayPlanInfo;
import com.niiwoo.civet.trade.dto.response.cardniu.RepayResultDTO;
import com.niiwoo.civet.trade.dto.response.fanlitou.FanLiTouInvestResponseDTO;
import com.niiwoo.civet.trade.dto.response.myloan.GetWithholdRepaymentResultDTO;
import com.niiwoo.civet.trade.enums.BorrowerPlanRefundStatusEnum;
import com.niiwoo.civet.trade.enums.CardNiuRefundStatusEnum;
import com.niiwoo.civet.trade.service.cardniu.CardNiuLoanDubboService;
import com.niiwoo.civet.trade.service.local.cardniu.CardNiuLoanLocalService;
import com.niiwoo.civet.trade.service.local.newRefund.common.RefundCommonService;
import com.niiwoo.civet.trade.service.local.newRefund.common.RefundLocalService;
import com.niiwoo.civet.trade.service.local.refund.CommonLocalService;
import com.niiwoo.civet.trade.utils.RepaymentUtil;
import com.niiwoo.civet.user.service.UserCreditDubboService;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by liujinping on 2018/7/17.
 */
@Service
@Slf4j
public class FanLiTouLocalService {

	@Autowired
	private ProjectInvestOrderMapperExt projectInvestOrderMapperExt;

	public FanLiTouInvestResponseDTO getInvestRecord(FanLiTouRequestDTO fanLiTouRequestDTO) {
		FanLiTouInvestResponseDTO responseDTO = new FanLiTouInvestResponseDTO();
		List<FanLiTouRequestDTO.UserRelation> userRelationList = fanLiTouRequestDTO.getUserRelationList();
		int count = projectInvestOrderMapperExt.selectTotalCountByUserList(userRelationList,fanLiTouRequestDTO.getStartTime(),fanLiTouRequestDTO.getEndTime());

		if(count>0) {
			List<ProjectInvestOrder> list = projectInvestOrderMapperExt.selectByDetailByUserList(userRelationList, fanLiTouRequestDTO.getStartTime(), fanLiTouRequestDTO.getEndTime(), fanLiTouRequestDTO.getOffset(), fanLiTouRequestDTO.getPageSize());
			if(!CollectionUtils.isEmpty(list)){
				List<ProjectInvestOrder> firstInvestList = projectInvestOrderMapperExt.selectFirstInvestByUserList(userRelationList);

				List<FanLiTouInvestResponseDTO.Orders> ordersList = new ArrayList<>();
				for(ProjectInvestOrder projectInvestOrder : list){
					FanLiTouInvestResponseDTO.Orders order = new FanLiTouInvestResponseDTO.Orders();

					for(FanLiTouRequestDTO.UserRelation userRelation : userRelationList){
						if(userRelation.getUserId().equals(projectInvestOrder.getUserId())){
							order.setPhone_num(userRelation.getMobileNo());
							order.setUid(userRelation.getUid());
							break;
						}
					}
					order.setBid_id(projectInvestOrder.getProjectId().toString());
					order.setBid_name(projectInvestOrder.getTitle());
					order.setInvest_amount(Double.valueOf(projectInvestOrder.getAmount().toString()));
					order.setInvest_time(projectInvestOrder.getCreateTime());
					order.setIs_first_invest(false);
					for(ProjectInvestOrder firstInvestOrder : firstInvestList){
						if(firstInvestOrder.getId() .equals(projectInvestOrder.getId())){
							order.setIs_first_invest(true);
							break;
						}
					}
					ordersList.add(order);
				}
				responseDTO.setBid_list(ordersList);
			}
		}
		responseDTO.setTotal_count(count);
		return responseDTO;
	}

	public FanLiTouInvestResponseDTO getRepayRecord(FanLiTouRequestDTO fanLiTouRequestDTO) {
		FanLiTouInvestResponseDTO responseDTO = new FanLiTouInvestResponseDTO();
		List<FanLiTouRequestDTO.UserRelation> userRelationList = fanLiTouRequestDTO.getUserRelationList();
		int count = projectInvestOrderMapperExt.selectRepayTotalCountByUserList(userRelationList,fanLiTouRequestDTO.getStartTime(),fanLiTouRequestDTO.getEndTime());

		if(count>0) {
			List<FanLiTouProjectInvestOrder> list = projectInvestOrderMapperExt.selectRepayRecordByUserList(userRelationList, fanLiTouRequestDTO.getStartTime(), fanLiTouRequestDTO.getEndTime(), fanLiTouRequestDTO.getOffset(), fanLiTouRequestDTO.getPageSize());
			if(!CollectionUtils.isEmpty(list)){

				List<FanLiTouInvestResponseDTO.Orders> ordersList = new ArrayList<>();
				for(FanLiTouProjectInvestOrder projectInvestOrder : list){
					FanLiTouInvestResponseDTO.Orders order = new FanLiTouInvestResponseDTO.Orders();

					for(FanLiTouRequestDTO.UserRelation userRelation : userRelationList){
						if(userRelation.getUserId().equals(projectInvestOrder.getUserId())){
							order.setPhone_num(userRelation.getMobileNo());
							order.setUid(userRelation.getUid());
							break;
						}
					}
					order.setBid_id(projectInvestOrder.getProjectId().toString());
					order.setBid_name(projectInvestOrder.getTitle());
					order.setInvest_amount(Double.valueOf(projectInvestOrder.getAmount().toString()));
					order.setInvest_time(projectInvestOrder.getCreateTime());
					order.setIs_advanced_repay(true);
					order.setAdvanced_repay_date(projectInvestOrder.getOverTime());
					ordersList.add(order);
				}
				responseDTO.setBid_list(ordersList);
			}
		}
		responseDTO.setTotal_count(count);
		return responseDTO;
	}
}